package compilers;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.TokenStream;

import compilers.myGrammarParser.smallc_program_return;

public class Main {	
	
	public static boolean testFileErrors(String file) {
		CharStream stream;
		try {			
			stream = new ANTLRFileStream(file);
			myGrammarLexer lexer = new myGrammarLexer(stream);
			TokenStream tokenStream = new CommonTokenStream(lexer); 
			myGrammarParser parser = new myGrammarParser(tokenStream);
			parser.smallc_program();
			return true;
		} catch (Exception e) {
			System.out.println("PARSE ERROR");
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean testFile(String file, String output) {
		String out1 = "Converting " + file + " to " + output;
		System.out.println(out1);
		for(int i=1;i<=out1.length();i++) {
			System.out.print("=");
		}
		System.out.println();
		boolean ret = false;
		
		CharStream stream;
		FileWriter fstream = null;
		BufferedWriter out = null;
		try {			
			stream = new ANTLRFileStream(file);
			myGrammarLexer lexer = new myGrammarLexer(stream);
			TokenStream tokenStream = new CommonTokenStream(lexer); 
			myGrammarParser parser = new myGrammarParser(tokenStream);
			
			smallc_program_return program = parser.smallc_program();
			
			fstream = new FileWriter(output);
			out = new BufferedWriter(fstream);
			
			program.root.generate(out, null);
			out.close();
			ret = true;
		} catch (Exception e) {
			if(out != null) {
				try {
					out.close();
				} catch (IOException e1) {
					//
				}
			}
			if(e.getMessage() != null) System.err.println(e.getMessage());
			else e.printStackTrace();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				
			}
		} catch (Error e) {
			if(out != null) {
				try {
					out.close();
				} catch (IOException e1) {
					//
				}
			}
			if(e.getMessage() != null) System.err.println(e.getMessage());
			else e.printStackTrace();
			try {
				Thread.sleep(100);
			} catch (InterruptedException e1) {
				
			}
		}
		
		for(int i=1;i<=out1.length();i++) {
			System.out.print("=");
		}
		System.out.println();
		System.out.println();
		
		return ret;
	}
	
	public static void main(String[] args) {
		if(args.length == 1) {
			if(args[0].equals("test")) {
				String[] files = new File(".").list();
				for(int i = 0;i<files.length;i++) {
					if(files[i].contains("error")) {
						testFile(files[i],"out.p");
					} else if(files[i].contains("warning")) {
						testFile(files[i],"out.p");
					}
					new File("out.p").delete();
				}
			}
		} else if(args.length == 2) {
			if(new File(args[0]).exists()) {
				if(!new File(args[1]).exists() || new File(args[1]).delete()) {
					testFile(args[0],args[1]);
				} else {
					System.err.println("Output file isn't writable");
				}
			} else {
				System.err.println("Source file doesn't exist");
			}
		} else {
			System.err.println("Wrong number of arguments");
		}
	}
}
